﻿//**************************************************
//CLASS : tcdc_trn_user_campaign
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_trn_user_campaign
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int user_campaign_id {get;set;}
     public int user_id {get;set;}
     public int product_id {get;set;}
     public int campaign_id {get;set;}
     public int member_type_id {get;set;}
     public int payment_detail_id {get;set;}
     public string barcode {get;set;}
     public string RFID {get;set;}
     public string reference_no {get;set;}
     public string start_date {get;set;}
     public string expire_date {get;set;}
     public string status {get;set;}
     public string create_date {get;set;}
     public int create_by {get;set;}
     public string update_date {get;set;}
     public int update_by {get;set;}

     public tcdc_trn_user_campaign() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_trn_user_campaign" + 
                       " WHERE user_campaign_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_trn_user_campaign obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_trn_user_campaign " + 
                       "WHERE 1 = 1 ";

          if(obj.user_campaign_id != null && 
             obj.user_campaign_id.ToString() != null && 
             obj.user_campaign_id.ToString() != "0")
          {
               sql += "AND user_campaign_id LIKE '%" + obj.user_campaign_id + "%' ";
          }
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               sql += "AND user_id LIKE '%" + obj.user_id + "%' ";
          }
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               sql += "AND product_id LIKE '%" + obj.product_id + "%' ";
          }
          if(obj.campaign_id != null && 
             obj.campaign_id.ToString() != null && 
             obj.campaign_id.ToString() != "0")
          {
               sql += "AND campaign_id LIKE '%" + obj.campaign_id + "%' ";
          }
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               sql += "AND member_type_id LIKE '%" + obj.member_type_id + "%' ";
          }
          if(obj.payment_detail_id != null && 
             obj.payment_detail_id.ToString() != null && 
             obj.payment_detail_id.ToString() != "0")
          {
               sql += "AND payment_detail_id LIKE '%" + obj.payment_detail_id + "%' ";
          }
          if(obj.barcode != null && 
             obj.barcode.ToString() != null && 
             obj.barcode.ToString() != "0")
          {
               sql += "AND barcode LIKE '%" + obj.barcode + "%' ";
          }
          if(obj.RFID != null && 
             obj.RFID.ToString() != null && 
             obj.RFID.ToString() != "0")
          {
               sql += "AND RFID LIKE '%" + obj.RFID + "%' ";
          }
          if(obj.reference_no != null && 
             obj.reference_no.ToString() != null && 
             obj.reference_no.ToString() != "0")
          {
               sql += "AND reference_no LIKE '%" + obj.reference_no + "%' ";
          }
          if(obj.start_date != null && 
             obj.start_date.ToString() != null && 
             obj.start_date.ToString() != "0")
          {
               sql += "AND start_date LIKE '%" + obj.start_date + "%' ";
          }
          if(obj.expire_date != null && 
             obj.expire_date.ToString() != null && 
             obj.expire_date.ToString() != "0")
          {
               sql += "AND expire_date LIKE '%" + obj.expire_date + "%' ";
          }
          if(obj.status != null && 
             obj.status.ToString() != null && 
             obj.status.ToString() != "0")
          {
               sql += "AND status LIKE '%" + obj.status + "%' ";
          }
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               sql += "AND create_date LIKE '%" + obj.create_date + "%' ";
          }
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               sql += "AND create_by LIKE '%" + obj.create_by + "%' ";
          }
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               sql += "AND update_date LIKE '%" + obj.update_date + "%' ";
          }
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               sql += "AND update_by LIKE '%" + obj.update_by + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_trn_user_campaign obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[16];

          prm[0] = new SqlParameter("@user_id", SqlDbType.Int);
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               prm[0].Value = obj.user_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@product_id", SqlDbType.Int);
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               prm[1].Value = obj.product_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@campaign_id", SqlDbType.Int);
          if(obj.campaign_id != null && 
             obj.campaign_id.ToString() != null && 
             obj.campaign_id.ToString() != "0")
          {
               prm[2].Value = obj.campaign_id;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@member_type_id", SqlDbType.Int);
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               prm[3].Value = obj.member_type_id;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          prm[4] = new SqlParameter("@payment_detail_id", SqlDbType.Int);
          if(obj.payment_detail_id != null && 
             obj.payment_detail_id.ToString() != null && 
             obj.payment_detail_id.ToString() != "0")
          {
               prm[4].Value = obj.payment_detail_id;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }

          prm[5] = new SqlParameter("@barcode", SqlDbType.NVarChar,50);
          if(obj.barcode != null && 
             obj.barcode.ToString() != null && 
             obj.barcode.ToString() != "0")
          {
               prm[5].Value = obj.barcode;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }

          prm[6] = new SqlParameter("@RFID", SqlDbType.NVarChar,50);
          if(obj.RFID != null && 
             obj.RFID.ToString() != null && 
             obj.RFID.ToString() != "0")
          {
               prm[6].Value = obj.RFID;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }

          prm[7] = new SqlParameter("@reference_no", SqlDbType.NVarChar,50);
          if(obj.reference_no != null && 
             obj.reference_no.ToString() != null && 
             obj.reference_no.ToString() != "0")
          {
               prm[7].Value = obj.reference_no;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }

          prm[8] = new SqlParameter("@start_date", SqlDbType.DateTime);
          if(obj.start_date != null && 
             obj.start_date.ToString() != null && 
             obj.start_date.ToString() != "0")
          {
               prm[8].Value = obj.start_date;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }

          prm[9] = new SqlParameter("@expire_date", SqlDbType.DateTime);
          if(obj.expire_date != null && 
             obj.expire_date.ToString() != null && 
             obj.expire_date.ToString() != "0")
          {
               prm[9].Value = obj.expire_date;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }

          prm[10] = new SqlParameter("@status", SqlDbType.NVarChar,50);
          if(obj.status != null && 
             obj.status.ToString() != null && 
             obj.status.ToString() != "0")
          {
               prm[10].Value = obj.status;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }

          prm[11] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[11].Value = obj.create_date;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }

          prm[12] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[12].Value = obj.create_by;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }

          prm[13] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[13].Value = obj.update_date;
          }
          else
          {
               prm[13].Value = DBNull.Value;
          }

          prm[14] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[14].Value = obj.update_by;
          }
          else
          {
               prm[14].Value = DBNull.Value;
          }

          prm[15] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[15].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_trn_user_campaign_insert", prm, false);
          return (Int32)prm[15].Value;
     }

     public int updateData(tcdc_trn_user_campaign obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[16];

          prm[0] = new SqlParameter("@user_campaign_id", SqlDbType.Int);
          if(obj.user_campaign_id != null && 
             obj.user_campaign_id.ToString() != null && 
             obj.user_campaign_id.ToString() != "0")
          {
               prm[0].Value = obj.user_campaign_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@user_id", SqlDbType.Int);
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               prm[1].Value = obj.user_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@product_id", SqlDbType.Int);
          if(obj.product_id != null && 
             obj.product_id.ToString() != null && 
             obj.product_id.ToString() != "0")
          {
               prm[2].Value = obj.product_id;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@campaign_id", SqlDbType.Int);
          if(obj.campaign_id != null && 
             obj.campaign_id.ToString() != null && 
             obj.campaign_id.ToString() != "0")
          {
               prm[3].Value = obj.campaign_id;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }
          prm[4] = new SqlParameter("@member_type_id", SqlDbType.Int);
          if(obj.member_type_id != null && 
             obj.member_type_id.ToString() != null && 
             obj.member_type_id.ToString() != "0")
          {
               prm[4].Value = obj.member_type_id;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }
          prm[5] = new SqlParameter("@payment_detail_id", SqlDbType.Int);
          if(obj.payment_detail_id != null && 
             obj.payment_detail_id.ToString() != null && 
             obj.payment_detail_id.ToString() != "0")
          {
               prm[5].Value = obj.payment_detail_id;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }
          prm[6] = new SqlParameter("@barcode", SqlDbType.NVarChar,50);
          if(obj.barcode != null && 
             obj.barcode.ToString() != null && 
             obj.barcode.ToString() != "0")
          {
               prm[6].Value = obj.barcode;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }
          prm[7] = new SqlParameter("@RFID", SqlDbType.NVarChar,50);
          if(obj.RFID != null && 
             obj.RFID.ToString() != null && 
             obj.RFID.ToString() != "0")
          {
               prm[7].Value = obj.RFID;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }
          prm[8] = new SqlParameter("@reference_no", SqlDbType.NVarChar,50);
          if(obj.reference_no != null && 
             obj.reference_no.ToString() != null && 
             obj.reference_no.ToString() != "0")
          {
               prm[8].Value = obj.reference_no;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }
          prm[9] = new SqlParameter("@start_date", SqlDbType.DateTime);
          if(obj.start_date != null && 
             obj.start_date.ToString() != null && 
             obj.start_date.ToString() != "0")
          {
               prm[9].Value = obj.start_date;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }
          prm[10] = new SqlParameter("@expire_date", SqlDbType.DateTime);
          if(obj.expire_date != null && 
             obj.expire_date.ToString() != null && 
             obj.expire_date.ToString() != "0")
          {
               prm[10].Value = obj.expire_date;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }
          prm[11] = new SqlParameter("@status", SqlDbType.NVarChar,50);
          if(obj.status != null && 
             obj.status.ToString() != null && 
             obj.status.ToString() != "0")
          {
               prm[11].Value = obj.status;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }
          prm[12] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[12].Value = obj.create_date;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }
          prm[13] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[13].Value = obj.create_by;
          }
          else
          {
               prm[13].Value = DBNull.Value;
          }
          prm[14] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[14].Value = obj.update_date;
          }
          else
          {
               prm[14].Value = DBNull.Value;
          }
          prm[15] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[15].Value = obj.update_by;
          }
          else
          {
               prm[15].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_trn_user_campaign_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@user_campaign_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_trn_user_campaign_delete", prm, false);
     }
}

}

